package com.marketingexpert.controller.marketingmgnt.itemrecomment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.config.SystemErrorCode;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.storemgnt.StoreServiceFactory;
import com.marketingexpert.util.Resources;
import com.marketingexpert.util.WebUtils;
import com.taobao.api.domain.Item;

public class GetNewItemController implements Controller {

	private StoreServiceFactory storeServiceFactory;

	private SystemErrorCode systemErrorCode;

	private String dispatchView;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long pageNo = 1L;

		if (request.getParameter("pageNo") != null
				&& !request.getParameter("pageNo").trim().equals("")) {
			pageNo = Long.parseLong(request.getParameter("pageNo"));
		}

		String sessionKey = (String) request.getSession().getAttribute(
				"taobaoSessionKey");

		try {
			List<Item> items = getStoreServiceFactory().getService(
					StoreServiceFactory.TAOBAO).getItemOnSale(pageNo,
					sessionKey);

			if (items != null && items.size() <= 0) {
				items = null;
			}
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("pageTotal", 30);
			request.setAttribute("its", items);
			request.setAttribute("forwordUrl", request.getParameter("forwordUrl"));
			return new ModelAndView(request.getParameter("forwordUrl"));
		} catch (MarketingExpertException e) {
			if (e.getErrCode().equals(systemErrorCode.getInvalidSession())
					|| e.getErrCode().equals(
							systemErrorCode.getTaobaoExpiredSession())) {
				request.getSession().removeAttribute("taobaoSessionKey");
				request.getSession().removeAttribute("parentUserId");
				request.getSession().setAttribute("sessionExpired",
						Resources.getString("InvalidSession"));
				response.sendRedirect(WebUtils.getBasepath(request)
						+ "/pages/sessionExpiredTip.jsp");
				return null;
			}

			request.setAttribute("getItemOnsaleFailed", Resources.getString("GetNewItemController.getItemOnsaleFailed"));
			return new ModelAndView(request.getParameter("forwordUrl"));
		}
	}

	public StoreServiceFactory getStoreServiceFactory() {
		return storeServiceFactory;
	}

	public void setStoreServiceFactory(StoreServiceFactory storeServiceFactory) {
		this.storeServiceFactory = storeServiceFactory;
	}

	public SystemErrorCode getSystemErrorCode() {
		return systemErrorCode;
	}

	public void setSystemErrorCode(SystemErrorCode systemErrorCode) {
		this.systemErrorCode = systemErrorCode;
	}

	public String getDispatchView() {
		return dispatchView;
	}

	public void setDispatchView(String dispatchView) {
		this.dispatchView = dispatchView;
	}

}
